import os
import re
import shutil
import tqdm


def px2rem(reg):
    if reg.group(0) == '0px':
        return '0rem'
    if reg.group(0) == '1px':
        return '1px'
    num = float(reg.group(1))
    return "%.6frem" % (num * 320 / 37500)


if __name__ == '__main__':
    input_file_list = os.listdir('./input/')
    if os.path.exists('./output/'):
        shutil.rmtree('./output/')
    os.mkdir('./output/')
    for input_file in tqdm.tqdm(input_file_list):
        output_file_line_list = []
        with open(f'./input/{input_file}', mode='r', encoding='utf-8') as input_f:
            input_file_line_list = input_f.readlines()
            for input_file_line in input_file_line_list:
                result = re.sub(r'([\d.]+)px', px2rem, input_file_line)
                output_file_line_list.append(result)
        with open(f'./output/{input_file}', mode='w', encoding='utf-8') as output_f:
            output_f.writelines(output_file_line_list)
